package co.id.vivastor.pantau;

import co.id.vivastor.pantau.util.Global;
import co.id.vivastor.pantau.util.SMSSender;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class KejadianActivity extends Activity {
	public static final String IDENTIFIER = "KEJADIAN";
	private int _selectedJenisKejadian;
	private String _waktuKejadian;
	private String _keterangan;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_kejadian);
		_setOtherViews();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_kejadian, menu);
		return true;
	}

	public void _setOtherViews() {
		//Icon Clickable goto home menu:
		ImageView icon = (ImageView) findViewById(R.id.imageView1);
		Global.setImageViewButtonListener(icon, KejadianActivity.this, MainActivity.class);
		
		//Set Spinner Jenis Kejadian
		Spinner spinnerJenisKejadian = (Spinner) findViewById(R.id.spinner_jenis_kejadian);
		ArrayAdapter<CharSequence> adapterJenisKejadian = ArrayAdapter.createFromResource(this, R.array.kejadian_jenis, R.layout.spinner_layout);
		adapterJenisKejadian.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerJenisKejadian.setAdapter(adapterJenisKejadian);
		spinnerJenisKejadian.setOnItemSelectedListener(
				new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						KejadianActivity.this._selectedJenisKejadian = (int)arg0.getItemIdAtPosition(arg2);
					}
					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
					}
				}
		);
		
		//Set TimePicker Waktu Kejadian
        TimePicker waktuKejadian = (TimePicker) findViewById(R.id.timePicker_waktu_kejadian);    
        waktuKejadian.setIs24HourView(true);
        KejadianActivity.this._waktuKejadian = waktuKejadian.getCurrentHour()+":"+waktuKejadian.getCurrentMinute();
    	waktuKejadian.setOnTimeChangedListener(
    			new OnTimeChangedListener() {					
					@Override
					public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
						KejadianActivity.this._waktuKejadian = arg1+":"+arg2;
						//Toast.makeText(getApplicationContext(), arg1 + " : " + arg2, Toast.LENGTH_LONG).show();
					}
				}
		);
    	
    	//Set Listener Send Button
    	Button btnSendSMS = (Button) findViewById(R.id.buttonSend);
		btnSendSMS.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Cek setting udah terisi:
				String kodeWilayah = Global.getData(getApplicationContext(), Global.KODE_WILAYAH, Global.DEFAULT_VALUE);
				String nomorTPS = Global.getData(getApplicationContext(), Global.NOMOR_TPS, Global.DEFAULT_VALUE);
				String SMSCenterNo = Global.getData(getApplicationContext(), Global.SMS_CENTER_NO, Global.DEFAULT_VALUE);
				
				if (kodeWilayah == Global.DEFAULT_VALUE || nomorTPS == Global.DEFAULT_VALUE || SMSCenterNo == Global.DEFAULT_VALUE ||
						kodeWilayah == "" || nomorTPS == "" || SMSCenterNo == "") {
					//Toast.makeText(getApplicationContext(), "Maaf, Setting belum terkonfigurasi", Toast.LENGTH_SHORT).show();
					Global.showDialog(KejadianActivity.this, "Konfigurasi", "Maaf, setting belum terkonfigurasi. Cek halaman Setting.", "Setting");
				} else {
					EditText  keterangan = (EditText) findViewById(R.id.editText_keterangan);
					KejadianActivity.this._keterangan = keterangan.getText().toString();
					
					SMSSender smsSender = new SMSSender(KejadianActivity.this);
					String message = KejadianActivity.IDENTIFIER+" "+kodeWilayah+" "+nomorTPS+" "+KejadianActivity.this._selectedJenisKejadian+" "+KejadianActivity.this._waktuKejadian+" "+KejadianActivity.this._keterangan;
					smsSender.sendSMS(SMSCenterNo, message);
				}
			}
		});
	}

}
